<?php 

namespace common\service;

use common\libs\BlockChain;
use think\Config;
class BlockWalletService
{
//    const COMMISSION_QRCODE_URL = 'https://api.qrserver.com/v1/create-qr-code/?size=155x155&data='; // 二维码生成链接
    const COMMISSION_QRCODE_URL = 'http://qr.liantu.com/api.php?&w=200&text='; // 二维码生成链接

    private $blackChain = null;
	
	public static $instance = null;
	
	private function __construct(){
	    $config = Config::get('blockwallet_config');
        $this->blackChain = new BlockChain();
        $this->blackChain->setPrivateKey($config['private_key']);
        $this->blackChain->setBot($config['bot']);
        $this->blackChain->setRequestUrl($config['request_url']);
        $this->blackChain->setCallBackWallet($config['callback_wallet']);
        $this->blackChain->setCallBackStatus($config['callback_status']);
	}

	public static function getInstance(){
		if(self::$instance == null){
			self::$instance = new self();
		}
		return self::$instance;
	}

	public function check($msg)
    {
        $msg = base64_decode($msg);
        $msg = json_decode($msg, true);
        if(empty($msg) || empty($msg['sign']) || empty($msg['user']) || empty($msg['wallet_address'])){
            return false;
        }

        $sign = $msg['sign'];
        unset($msg['sign']);

        if($this->blackChain->getSign($msg) != $sign){
            return false;
        }
        return $msg;
    }

	public function getQRcode($params)
    {
        $params['callback'] = $this->blackChain->getCallBackWallet();
        $sign = $this->blackChain->getSign($params);
        $params['sign'] = $sign;
        $params = json_encode($params);
        $msg = base64_encode($params);
        $url = $this->blackChain->getWalletUrl($msg);
        return self::COMMISSION_QRCODE_URL.urlencode($url);
    }

}